<?php
namespace app\spadmin\controller\user;
use app\common\controller\Backend;
use app\common\model\RecordRecharge;
use app\common\model\RecordTrade;
use think\Db;
use think\Exception;
class Account extends Backend{

    private $userLevel;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Account');
    }
    public function index(){

        $this->request->filter(['strip_tags', 'trim']);
        if($this->request->isAjax()){
            $data = $this->request->param();
            if (isset($data['keywords']) && $data['keywords']) {
                $map['account_name|account_card|account_phone'] = ['like','%'.$data['keywords'].'%'];
            }           

            $map['status'] = 1;      
            $list =   $this->model->where($map)->order('id desc')->paginate($data['limit'])->toArray();
           
            foreach ($list['data'] as $key => &$value) {
                if(Db::name('User')->where('account_id',$value['id'])->find()){
                    $value['union']='已绑微信';
                }else{
                    $value['union']='未绑微信';
                }
            }
            return $this->result($list);
        }
        return $this->view->fetch();
    }
    public function detail($ids=null){
        $row = $this->model->get($ids);
        if(Db::name('User')->where('account_id',$row['id'])->find()){
            $row['union']='已绑微信';
        }else{
            $row['union']='未绑微信';
        }
        $this->assign('info',$row);
        return $this->view->fetch();
    }
    public function edit($ids=null){
        $row = $this->model->get($ids);
        if($this->request->isPost()){
            $data = $this->request->post('',[],'strip_tags');
            if(empty($data['id']) && empty($data['account_card'])){
                $this->error('新建账户必须有卡号');
            }
            if($row == null){
                $row = $this->model;
            }
            
            $result = $row->allowField(true)->save($data,$data['id']?['id'=>$data['id']]:null);
            if($result === false){
                $this->error($row->getError());
            }
            $this->success('操作成功');            
        }else{
            $this->assign('info',$row);
            return $this->view->fetch();
        }
    }

    public function Recharge($ids=null){
        $row = $this->model->get($ids);
        if($this->request->isPost()){
            $data = $this->request->post('',[],'strip_tags');
            if(empty($data['id'])){
                $this->error('未知账户');
            }
            if(empty($data['recharge_amount']) || empty($data['extra_amount'])){
                $this->error('充值金额为0,无法充值');
            }
            
            Db::startTrans();
            try {
                if(!empty($data['recharge_amount'])){
                    $trade = new RecordTrade([
                        'account_id'=>$row['id'],
                        'type'=>'recharge',
                        'is_extra'=>0,
                        'before_amount'=>$row['balance'],
                        'amount'=>$data['recharge_amount'],
                        'chanage_amount'=> (string)$data['recharge_amount'],
                        'after_amount'=>round($row['balance']+$data['recharge_amount'],2),
                        'operator'=>'admin'
                    ]);
                    $trade->save();
                    $row['balance'] = round($row['balance']+$data['recharge_amount'],2);
                }
                if(!empty($data['extra_amount'])){
                    $extra_trade = new RecordTrade([
                        'account_id'=>$row['id'],
                        'type'=>'recharge',
                        'is_extra'=>1,
                        'before_amount'=>$row['extra_amount'],
                        'amount'=>$data['extra_amount'],
                        'chanage_amount'=> (string)$data['extra_amount'],
                        'after_amount'=>round($row['extra_amount']+$data['extra_amount'],2),
                        'operator'=>'admin'
                    ]);
                    $extra_trade->save();
                    $row['extra_amount'] = round($row['extra_amount'] + $data['extra_amount'],2) ;
                }
                $recharge = new RecordRecharge([
                    'account_id'=>$row['id'],
                    'order_id'=>0,
                    'recharge_amount'=>$data['recharge_amount']?:0,
                    'extra_amount'=>$data['extra_amount']?:0,
                    'recharge_type'=>3,
                    'operator'=>'admin'
                ]);
                $recharge->save();
                $row->save();
                Db::commit();
                $this->success('操作成功');
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }        
        }else{
            $this->assign('info',$row);
            return $this->view->fetch();
        }
    }
   
}